package com.jy.servlet;

import com.google.gson.Gson;
import com.jy.dao.UserDao;
import com.jy.dao.impl.UserDaoImpl;
import com.jy.entity.Classes;
import com.jy.entity.Student;
import com.jy.entity.Teacher;
import com.jy.entity.register;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/no/*")
public class NoLoginServlet extends BaseServlet {
	UserDao user=new UserDaoImpl();
	//学生登录
	protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("stname");
		String pass=request.getParameter("stpass");
		System.out.println(name+"--"+pass);
		Student stu= null;
		try {
			stu=user.login(name,pass);
			if(stu!=null){
				request.getSession().setAttribute("stuid",stu.getStid());
				request.getSession().setAttribute("act",name);
				request.getSession().setAttribute("sf","student");
				System.out.println("学生,登录成功");
				response.sendRedirect("/examproject/nologin/index.jsp");
			}else{
				System.out.println("登录失败");
				response.sendRedirect("/examproject/nologin/LogInTo.jsp?msg=false");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}


	}

	//老师登录
	protected void login2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tname=request.getParameter("stname");
		String tpss=request.getParameter("stpass");
		Teacher tc=null;
		try {
			tc=user.login2(tname,tpss);
			if(tc!=null){
				request.getSession().setAttribute("tid",tc.getTeacherId());
				request.getSession().setAttribute("act",tname);
				request.getSession().setAttribute("sf","teacher");
				System.out.println("老师,登录成功");
				response.sendRedirect("/examproject/nologin/index.jsp");
			}else{
				System.out.println("登录失败");
				response.sendRedirect("/examproject/nologin/LogInTo.jsp?msg=false");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//学生找回密码
	protected void pwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String stname = request.getParameter("stname");
		String tel = request.getParameter("tel");
		System.out.println("传学生找回密码："+stname + "--" + tel);
		Student stu = null;
		try {
			stu = user.pwd(stname, tel);
			if (stu == null) {
				System.out.println("错误");
				response.sendRedirect("/examproject/nologin/getpass.jsp");
			} else {
				System.out.println("正确");
				HttpSession session = request.getSession();
				session.setAttribute("name", stname);
				response.sendRedirect("/examproject/nologin/newpwd.jsp");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//学生修改密码
	protected void stupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pwd = request.getParameter("pwd");
		String name = request.getParameter("name");
		System.out.println("学生修改密码："+pwd + "--" + name);
		Student stu = new Student();
		stu.setStpass(pwd);
		stu.setStname(name);
		int n = 0;
		try {
			n = user.stupdate(stu);
			if (n > 0) {
				System.out.println("修改成功");
				response.sendRedirect("/examproject/nologin/LogInTo.jsp");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//老师找回密码
	protected void pwd2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tname = request.getParameter("stname");
		String tel = request.getParameter("tel");
		System.out.println("传到老师找回密码"+tname + "--" + tel);
		Teacher tc = null;
		try {
			tc = user.pwd2(tname, tel);
			if (tc == null) {
				System.out.println("错误");
				response.sendRedirect("/examproject/nologin/getpass.jsp");
			} else {
				System.out.println("正确");
				String yz="lsyz";
				HttpSession session = request.getSession();
				session.setAttribute("name", tname);
				session.setAttribute("lsyz",yz);
				response.sendRedirect("/examproject/nologin/newpwd.jsp");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//老师修改密码
	protected void supdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pwd = request.getParameter("pwd");
		String name = request.getParameter("name");
		System.out.println("老师修改密码："+pwd + "--" + name);
		Teacher tc = new Teacher();
		tc.setTpass(pwd);
		tc.setTname(name);
		int n = 0;
		try {
			n = user.tcupdate(tc);
			if (n > 0) {
				System.out.println("修改成功");
				response.sendRedirect("/examproject/nologin/LogInTo.jsp");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//注册学生
	protected void addstu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDao user = new UserDaoImpl();
		Student s=new Student();
		String classId=request.getParameter("classId");
		String stname=request.getParameter("stname");
		String stpass=request.getParameter("stpass");
		String zcm=request.getParameter("zcm");
		String sex=request.getParameter("sex");
		System.out.println(classId);
		System.out.println("性别:"+sex);
		s.setClassid(Integer.parseInt(classId));
		s.setStname(stname);
		s.setStpass(stpass);
		s.setStsex(sex);
		int c= 0;
		try {
			List list=user.getyh(stname);
			System.out.println(list);
			register reg=user.getzcm();
			System.out.println(reg.getZcm_stu());
			if(list.size()!=0){
				response.sendRedirect("/examproject/nologin/Register.jsp?msg=true");
			}else if(!reg.getZcm_stu().equals(zcm)) {
				response.sendRedirect("/examproject/nologin/Register.jsp?msg=false");
			}else{
				c = user.addstu(s);
				if(c>0) {
					response.sendRedirect("/examproject/nologin/LogInTo.jsp");
				}
			}
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(c);


	}

	Gson gson=new Gson();
	//查班级
	protected void cls(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//	response.setContentType("text/html");
		response.setContentType("application/json");
		String sql="select *from classes";
		try {
			List c=user.getclasses(sql);
			System.out.println(c);
			String reg=gson.toJson(c);
			PrintWriter out=response.getWriter();
			out.print(reg);
			out.flush();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
//注册老师
	protected void addtea(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDao user = new UserDaoImpl();
		Teacher t=new Teacher();
		String stname=request.getParameter("stname");
		String stpass=request.getParameter("stpass");
		String zcm=request.getParameter("zcm");
		t.setTname(stname);
		t.setTpass(stpass);
		int c= 0;
		try {
			List list=user.getls(stname);
			System.out.println(list);
			register reg=user.getzcm();
			System.out.println(reg.getZcm_tea());
			if(list.size()!=0){
				response.sendRedirect("/examproject/nologin/Register1.jsp?msg=true");
			}else if(!reg.getZcm_tea().equals(zcm)) {
				response.sendRedirect("/examproject/nologin/Register1.jsp?msg=false");
			}else{
				c = user.addtea(t);
				if(c>0) {
					response.sendRedirect("/examproject/nologin/LogInTo.jsp");
				}
			}
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(c);
	}

//退出登陆
	protected void remove(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.getSession().setAttribute("act",null);
		response.sendRedirect("/examproject/nologin/index.jsp");
	}
}
